/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.battletank.menu;

import com.nokia.example.battletank.game.Resources;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class Menu {
    public static final int POINTER_PRESSED = 0;
    public static final int POINTER_DRAGGED = 1;
    public static final int POINTER_RELEASED = 2;

    private String resourcePath;

    private final MenuItem[] items;
    private final Listener listener;

    public volatile boolean visible = false;

    protected Menu(int capacity, int w, int h, Listener listener) {
        items = new MenuItem[capacity];
        if(Math.max(w, h) < Resources.MEDIUM_THRESHOLD) {
            resourcePath = "/menu/low/";
        } else {
            resourcePath = "/menu/medium/";
        }
        this.listener = listener;
    }

    protected final int getSize() {
        return items.length;
    }
    
    protected final void setItem(int i, MenuItem item) {
        items[i] = item;
    }

    protected final MenuItem getItem(int i) {
        return items[i];
    }

    public final void render(Graphics g) {
        if(visible) paint(g);
    }

    protected void paint(Graphics g) {
        for(int i = 0; i < items.length; i++) {
            items[i].paint(g);
        }
    }

    public void selectItem(int selected) {
        for(int i = 0; i < items.length; i++) {
            items[i].setSelected(i == selected);
        }
    }

    private int getSelected() {
        for(int i = 0; i < items.length; i++) {
            if(items[i].isSelected()) return i;
        }
        return -1;
    }

    public void selectNext() {
        selectItem((getSelected() + 1)%items.length);
    }

    public void selectPrev() {
        selectItem((Math.max(getSelected(), 0) - 1 + items.length)%items.length);
    }

    public void clickSelected() {
        clickItem(getSelected());
    }
    
    private void clickItem(int index) {
        if(index > -1 && index < items.length) {
            listener.itemClicked(index);
            items[index].setSelected(false);
        }
    }

    public void pointerEvent(int type, int x, int y) {
        for(int i = 0; i < items.length; i++) {
            if(items[i].hits(x, y)) {
                if(type == POINTER_RELEASED) {
                    clickItem(i);
                } else {
                    selectItem(i);
                }
                return;
            } 
        }
        selectItem(-1);
    }

    protected Sprite loadSprite(String fileName, int lines) {
        Image i = loadImage(fileName);
        return new Sprite(i, i.getWidth(), i.getHeight()/lines);
    }

    protected Image loadImage(String fileName) {
        try {
            return Image.createImage(resourcePath + fileName);
        } catch (IOException e) {
            return null;
        }
    }

    public interface Listener {
        void itemClicked(int item);
    }
}
